package logic

import (
	"context"
	"errors"
	"video/mysql/model"
	"video/pkg"

	"video/videorpc/internal/svc"
	"video/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 评论添加
func (l *CommentAddLogic) CommentAdd(in *videorpc.CommentAddRequest) (*videorpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line

	var user model.User
	model.DB.Debug().Where("id = ?", in.UserId).Find(&user)
	if user.ID == 0 {
		return nil, errors.New("请先登录")
	}

	if in.Content == "" {
		return nil, errors.New("请输入评论内容")
	}

	comment := model.Comment{
		VideoId: in.VideoId,
		UserId:  in.UserId,
		Content: in.Content,
	}

	if pkg.CheckWord(in.Content) {
		return nil, errors.New("当前评论内容包含敏感词，评论失败")
	}

	model.DB.Debug().Create(&comment)
	return &videorpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
